#!/usr/bin/perl
# Version 0.2.1

$testcase = "e_bit";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 4) {
        print("Usage: $testcase.pl dump_file1 dump_file2 dump_file3 dump_file4 log_file");
        exit 1;
}

$dumpfile[0] = $ARGV[0];
$dumpfile[1] = $ARGV[1];
$dumpfile[2] = $ARGV[2];
$dumpfile[3] = $ARGV[3];
$logfile = $ARGV[4];

#------------------------------------------------------------------------------
@local_verdict = ( 3, 3, 3, 3);

for( $i = 0; $i < 4; $i++) {
	# Walk through the logfile. 
	$success = 1;
	
	open(FILE, $dumpfile[$i]) or $success = 0;
	
	if($success eq 1) {
		
		if(($dumpfile[$i] eq "router2_2.dump") || ($dumpfile[$i] eq "router4_2.dump")) {
			# the logs of routers 2:2 and 3:2 (stub networks) mustn't
			# contain other packets than hellos and no neighbors should be
			# contained in those hellos
			
			$local_verdict[$i] = 1;
		
			while(<FILE>) {
				if($_ =~ /OSPFv2/) {
					# look only at OSPF packets
					if( $_ =~ /hello/ ) {
						if( $_ =~ /nbrs/ ) {
							# hello packet contains neighbors
							$local_verdict[$i] = 2;
						}
					} else {
						# not a hello packet
						$local_verdict[$i] = 2;
					}
				} 
			}
			
		} else {
			# the other logs should show the two routers becoming neighbors
			# after a while - therefore only look at hello packets sent
			# after RouterDeadInterval, to assure, that the routers have
			# seen each other
			
			$local_verdict[$i] = 1;
			
			while(<FILE>) {
				
				if($_ =~ /OSPFv2-hello/) {
					$dead = 40;
					if($_ =~ /dead (\d*)/) {
						$dead = $1;
					}
					
					# look only at OSPF hello packets
					if( $_ !~ /nbrs/ ) {
						@al = split(/ /, $_);
						
						# now look at the time
						@ti = split(/\./, $al[0]);
						@ts = split(/:/, $ti[0]);
			
						$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60;
						
						if($secs > $dead) {
							
							$local_verdict[$i] = 2;
						}
					}
				}
			}
		}
		
		close(FILE);
	}
	
	if ($local_verdict[$i] eq 1) {
		print("Evaluation of $dumpfile[$i] done. Test PASSED.\n");		
		
		if($i eq 0) {
			$verdict = 1;
		}
		
	} elsif ($local_verdict[$i] eq 2) {
		print("Evaluation of $dumpfile[$i] done. Errors were found. Test FAILED.\n");
		
		$verdict = 2;
	} elsif ($local_verdict[$i] eq 3) {
		print("$dumpfile[$i] cannot be evaluated. Test INCONCLUSIVE.\n");
		
		if($verdict ne 2) {
			$verdict = 3;
		}
	}
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");